#include "platestackinfowid.h"
#include <QTimer>
#include <QMessageBox>
#include "componentmanager.h"
#include "other/platestack.h"

PlateStackInfoWid::PlateStackInfoWid(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
}

PlateStackInfoWid::~PlateStackInfoWid()
{}


bool PlateStackInfoWid::Init()
{
	m_stack = dynamic_cast<PlateStack*>(component->GetComponent(Component::component_plateStack));
	if (m_stack == nullptr) {
		return false;
	}

	ui.lineEdit->setText(m_stack->GetPlateStackId());
	ShowState();
	return true;
}

void PlateStackInfoWid::ShowState()
{
	auto state = m_stack->GetPlateStackState();
	if (state.num < 0) {
		ui.lineEdit_2->setText("托盘数量无效");
	} else {
		ui.lineEdit_2->setText(QString::number(state.num));
	}

	switch (state.apply) {
	case PlateApplyState::not_need_apply:
		ui.label_6->setText("托盘数量大于0，无需申请托盘垛");
		break;
	case PlateApplyState::need_apply:
		ui.label_6->setText("正在申请托盘垛");
		break;
	case PlateApplyState::apply_failed:
		ui.label_6->setText("发送托盘垛申请失败");
		break;
	case PlateApplyState::apply_finish:
		ui.label_6->setText("申请完成，等待托盘垛到位");
		break;
	default:
		break;
	}

	QTimer::singleShot(1000, [&]() {
		ShowState();
		});
}

// 手动发送托盘垛申请
void PlateStackInfoWid::on_pushButton_clicked()
{
	QString msg = m_stack->ManualSendPlateApply();
	QMessageBox::warning(this, "提示", msg);
}
